extends Control

# CardTest.gd - 卡牌测试场景脚本
# 用于测试卡牌的显示、拖拽和基本功能

# 节点引用
@onready var hand_area = $HandArea
@onready var board_area = $BoardArea
@onready var info_label = $InfoPanel/InfoLabel
@onready var create_button = $CreateCardsButton
@onready var clear_button = $ClearButton

# 卡牌预制场景
var card_scene = preload("res://scenes/Card.tscn")

# 测试数据
var test_cards_data = [
	{
		"name": "骑士",
		"cost": 3,
		"attack": 3,
		"health": 5,
		"description": "勇敢的骑士，拥有坚固的护甲",
		"type": "unit",
		"rarity": "common",
		"image_path": "res://assets/images/knight.png"
	},
	{
		"name": "火球术",
		"cost": 4,
		"attack": 0,
		"health": 0,
		"description": "对目标造成6点伤害",
		"type": "spell",
		"rarity": "common",
		"image_path": "res://assets/images/fireball.png"
	},
	{
		"name": "精灵射手",
		"cost": 2,
		"attack": 2,
		"health": 1,
		"description": "敏捷的精灵射手",
		"type": "unit",
		"rarity": "common",
		"image_path": "res://assets/images/elf_archer.png"
	},
	{
		"name": "巨龙",
		"cost": 8,
		"attack": 8,
		"health": 8,
		"description": "强大的古龙，拥有毁灭性的力量",
		"type": "unit",
		"rarity": "legendary",
		"image_path": "res://assets/images/dragon.png"
	}
]

# 游戏状态
var current_mana = 10
var hand_cards = []
var board_cards = []

func _ready():
	"""初始化测试场景"""
	
	# 连接按钮信号
	create_button.pressed.connect(_on_create_cards_pressed)
	clear_button.pressed.connect(_on_clear_pressed)
	
	# 更新信息显示
	update_info_display()
	
	# 创建一些初始测试卡牌
	create_test_cards()

func create_test_cards():
	"""创建测试卡牌"""
	
	# 创建几张测试卡牌
	for i in range(min(4, test_cards_data.size())):
		create_card(test_cards_data[i])

func create_card(card_data: Dictionary):
	"""创建单张卡牌"""
	var card_instance = card_scene.instantiate()
	
	# 添加到手牌区域
	hand_area.add_child(card_instance)
	hand_cards.append(card_instance)
	
	# 设置卡牌数据
	card_instance.set_card_data(card_data)
	
	# 设置卡牌状态
	card_instance.is_in_hand = true
	card_instance.can_be_played = true
	
	# 连接卡牌信号
	card_instance.card_clicked.connect(_on_card_clicked)
	card_instance.card_drag_started.connect(_on_card_drag_started)
	card_instance.card_drag_ended.connect(_on_card_drag_ended)
	
	# 更新信息显示
	update_info_display()
	
func _on_card_clicked(card):
	"""处理卡牌点击事件"""
	
	# 更新信息面板显示卡牌详情
	var info_text = "卡牌信息:\n\n"
	info_text += "名称: " + card.card_name + "\n"
	info_text += "费用: " + str(card.cost) + "\n"
	info_text += "类型: " + card.card_type + "\n"
	info_text += "稀有度: " + card.rarity + "\n"
	info_text += "描述: " + card.description + "\n\n"
	
	if card.card_type == "unit":
		info_text += "攻击力: " + str(card.attack) + "\n"
		info_text += "生命值: " + str(card.health) + "\n\n"
	
	info_text += "当前手牌: " + str(hand_cards.size()) + "\n"
	info_text += "当前法力: " + str(current_mana)
	
	info_label.text = info_text

func _on_card_drag_started(card):
	"""处理卡牌拖拽开始事件"""
	pass

func _on_card_drag_ended(card, dropped_on_board: bool):
	"""处理卡牌拖拽结束事件"""
	
	if dropped_on_board and card.cost <= current_mana:
		# 将卡牌从手牌移动到战场
		move_card_to_board(card)
	elif dropped_on_board:
		print("法力不足，无法打出卡牌")

func move_card_to_board(card):
	"""将卡牌移动到战场"""
	if board_cards.size() >= 8:  # 战场最多8张卡
		print("战场已满，无法放置更多卡牌")
		return
	
	# 从手牌移除
	hand_cards.erase(card)
	hand_area.remove_child(card)
	
	# 添加到战场
	board_area.add_child(card)
	board_cards.append(card)
	
	# 消耗法力
	current_mana -= card.cost
	
	# 更新卡牌状态（在战场上）
	card.is_on_board = true
	
	# 更新信息显示
	update_info_display()

func update_info_display():
	"""更新信息显示"""
	var info_text = "卡牌信息:\n\n"
	info_text += "点击卡牌查看详情\n"
	info_text += "拖拽卡牌到战场\n\n"
	info_text += "当前手牌: " + str(hand_cards.size()) + "\n"
	info_text += "战场卡牌: " + str(board_cards.size()) + "\n"
	info_text += "当前法力: " + str(current_mana)
	
	info_label.text = info_text

func _on_create_cards_pressed():
	"""处理创建卡牌按钮点击"""
	
	# 随机创建一张测试卡牌
	var random_card_data = test_cards_data[randi() % test_cards_data.size()]
	create_card(random_card_data)

func _on_clear_pressed():
	"""处理清空按钮点击"""
	
	# 清空所有卡牌
	clear_all_cards()
	
	# 重置法力
	current_mana = 10
	
	# 更新信息显示
	update_info_display()

func clear_all_cards():
	"""清空所有卡牌"""
	# 清空手牌
	for card in hand_cards:
		card.queue_free()
	hand_cards.clear()
	
	# 清空战场
	for card in board_cards:
		card.queue_free()
	board_cards.clear()
	
func _input(event):
	"""处理输入事件"""
	if event is InputEventKey and event.pressed:
		match event.keycode:
			KEY_SPACE:
				# 空格键创建随机卡牌
				_on_create_cards_pressed()
			KEY_C:
				# C键清空
				_on_clear_pressed()
			KEY_M:
				# M键增加法力
				current_mana = min(current_mana + 1, 10)
				update_info_display()
